<template>
  <div class="login-form-container">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="80px" class="login-form" @keyup.enter="onLogin">
      <h2 class="form-title">用户登录</h2>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" placeholder="请输入邮箱" clearable />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" type="password" placeholder="请输入密码" show-password />
      </el-form-item>
      <div class="row remember-row">
        <el-checkbox v-model="rememberMe">记住我</el-checkbox>
        <a class="forgot-link" @click.prevent="onForgot">忘记密码?</a>
      </div>
      <el-form-item>
        <el-button :loading="loading" type="primary" class="login-btn" @click="onLogin">登录</el-button>
      </el-form-item>
      <transition name="fade">
        <div v-if="error" class="error-msg">{{ error }}</div>
      </transition>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useUserStore } from '@/stores/auth/user'
import { useRouter } from 'vue-router'

const userStore = useUserStore()
const router = useRouter()

const formRef = ref()
const form = ref({
  email: '',
  password: ''
})
const rememberMe = ref(false)
const loading = ref(false)
const error = ref('')

const STORAGE_KEY = 'app_login_cache'

onMounted(() => {
  try {
    const raw = localStorage.getItem(STORAGE_KEY)
    if (raw) {
      const cached = JSON.parse(raw)
      if (cached?.email) form.value.email = cached.email
      if (cached?.remember) rememberMe.value = true
    }
  } catch {}
})

const rules = {
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '邮箱格式不正确', trigger: ['blur', 'change'] }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度 6-20 位', trigger: 'blur' }
  ]
}

const onLogin = async () => {
  if (!formRef.value) return
  error.value = ''
  try {
    await (formRef.value as any).validate()
  } catch (e) {
    error.value = '请完善表单信息'
    return
  }
  loading.value = true
  try {
    await userStore.login({ email: form.value.email, password: form.value.password })
    if (rememberMe.value) {
      localStorage.setItem(STORAGE_KEY, JSON.stringify({ email: form.value.email, remember: true }))
    } else {
      localStorage.removeItem(STORAGE_KEY)
    }
    if(userStore.user.role === 'USER'){
      router.push('/user-center')
      return
    }
    else {
      router.push('/admin-dashboard')
      return
    }
  } catch (e: any) {
    error.value = e?.message || '登录失败'
  } finally {
    loading.value = false
  }
}

const onForgot = () => {
  // 跳转或弹出找回密码流程，这里先简单打印
  console.log('跳转到找回密码页')
  router.push('/reset') // 若你还没有该路由，可以先注释掉
}
</script>

<style scoped>
.login-form-container {
  max-width: 400px;
  margin: 80px auto;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 24px 0 rgba(0, 123, 255, 0.08);
  padding: 40px 32px 24px 32px;
  border: 1px solid #e6f0fa;
}
.login-form { --el-color-primary: #1976d2; }
.form-title {
  text-align: center;
  margin: 0 0 12px;
  font-size: 22px;
  font-weight: 600;
  background: linear-gradient(90deg,#1976d2,#42a5f5);
  background-clip: text;
  -webkit-background-clip: text;
  color: transparent;
}
.remember-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: -4px 0 8px 0;
  font-size: 13px;
  padding: 0 2px;
}
.forgot-link {
  color: #1976d2;
  cursor: pointer;
  text-decoration: none;
  transition: color .2s;
}
.forgot-link:hover { color: #125a9c; }
.login-btn {
  width: 100%;
  background: linear-gradient(90deg, #1976d2 0%, #42a5f5 100%);
  border: none;
  color: #fff;
  font-weight: bold;
  font-size: 16px;
  border-radius: 6px;
  box-shadow: 0 2px 8px 0 rgba(25, 118, 210, 0.12);
}
.login-btn:hover { background: linear-gradient(90deg, #1565c0 0%, #1e88e5 100%); }
.error-msg {
  margin-top: -8px;
  padding: 8px 12px;
  border-radius: 6px;
  background: #fdecea;
  color: #d93025;
  font-size: 13px;
  animation: shake 0.3s;
}
.fade-enter-active, .fade-leave-active { transition: opacity .25s; }
.fade-enter-from, .fade-leave-to { opacity: 0; }
@keyframes shake {
  0%,100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  50% { transform: translateX(4px); }
  75% { transform: translateX(-2px); }
}
</style>